//
//  BannerCell.m
//  Dianping
//
//  Created by 祝文博 on 15/3/10.
//  Copyright (c) 2015年 KevinLab. All rights reserved.
//

#import "BannerCell.h"

@interface BannerCell ()

@property (nonatomic, strong) BannerSlidingView *bannerSlidingView;

@end

@implementation BannerCell

+ (CGFloat)cellHeightWithBanner:(Banner *)banner
{
    return banner.image.size.height / 2.0;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setBanners:(NSArray *)banners
{
    if ([self isBanners:_banners sameWithBanners:banners] == NO) {
        _banners = banners;
        
        [self.bannerSlidingView removeFromSuperview];
        if (banners.count > 0) {
            self.bannerSlidingView = [[BannerSlidingView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, [[self class] cellHeightWithBanner:banners[0]]) banners:banners];
            [self.contentView addSubview:self.bannerSlidingView];
        }
    }
}

- (BOOL)isBanners:(NSArray *)banners1 sameWithBanners:(NSArray *)banners2
{
    if (banners1.count != banners2.count) {
        return NO;
    } else {
        for (int i = 0; i < banners1.count; i++) {
            Banner *banner1 = banners1[i];
            Banner *banner2 = banners2[i];
            if ([banner1.targetURL isEqualToString:banner2.targetURL] == NO) {
                return NO;
            }
        }
    }
    return YES;
}

@end
